//    Copyright (C) Clear Office.  All rights reserved.
using System;
using Office.Core.Serialization;
using Office.Spreadsheet.Serialization;

namespace Office.Spreadsheet.PivotTable
{
    class PivotSelection : IOfficeXmlSerializable
    {
        private PaneType _pane;
        private bool _showHeader;
        private bool _label;
        private bool _data;
        private bool _extendable;
        private int _count;
        private Axis? _axis;
        private int _dimension;
        private int _start;
        private int _min;
        private int _max;
        private int _activeRow;
        private int _activeCol;
        private int _previousRow;
        private int _previousCol;
        private int _click
                    ;

        private PivotArea _pivotArea;
        private PivotTableDefinition _definition;
        public PivotTableDefinition Definition{get{ return _definition;}}
        private string xmlName = "pivotSelection";

        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read(xmlName))
            {
                _pane = c.GetOptionalEnum("pane", PaneType.TopLeft);
                _showHeader = c.GetOptionalBoolean("showHeader", false);
                _label = c.GetOptionalBoolean("label", false);
                _data = c.GetOptionalBoolean("data", false);
                _extendable = c.GetOptionalBoolean("extendable", false);
                _count = c.GetOptionalInteger("count", 0);
                _axis = c.GetOptionalEnum<Axis>("axis");
                _dimension = c.GetOptionalInteger("dimension", 0);
                _start = c.GetOptionalInteger("start", 0);
                _min = c.GetOptionalInteger("min", 0);
                _max = c.GetOptionalInteger("max", 0);
                _activeRow = c.GetOptionalInteger("activeRow", 0);
                _activeCol = c.GetOptionalInteger("activeCol", 0);
                _previousRow = c.GetOptionalInteger("previousRow", 0);
                _previousCol = c.GetOptionalInteger("previousCol", 0);
                _click = c.GetOptionalInteger("click", 0);
                string rid = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "id")
                    ;
                if (rid != null)
                {
                    using (var c2 = c.OpenRelatedContext(rid, SpreadsheetConstants.Spreadsheet.NamespaceUri))
                    {
                        _definition = c2.ReadElement<PivotTableDefinition>();
                    }
                }
                _pivotArea = c.ReadOptionalElement<PivotArea>("pivotArea");
            }

        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write(xmlName))
            {
                c.SetOptionalEnum("pane", _pane, PaneType.TopLeft);
                c.SetOptionalBoolean("showHeader", _showHeader, false);
                c.SetOptionalBoolean("label", _label, false);
                c.SetOptionalBoolean("data", _data, false);
                c.SetOptionalBoolean("extendable", _extendable, false);
                c.SetOptionalInteger("count", _count, 0);
                c.SetOptionalEnum("axis", _axis);
                c.SetOptionalInteger("dimension", _dimension, 0);
                c.SetOptionalInteger("start", _start, 0);
                c.SetOptionalInteger("min", _min, 0);
                c.SetOptionalInteger("max", _max, 0);
                c.SetOptionalInteger("activeRow", _activeRow, 0);
                c.SetOptionalInteger("activeCol", _activeCol, 0);
                c.SetOptionalInteger("previousRow", _previousRow, 0);
                c.SetOptionalInteger("previousCol", _previousCol, 0);
                c.SetOptionalInteger("click", _click, 0);
                if (_definition != null)
                {

                    using (var c2 = c.CreateRelatedContext(SpreadsheetConstants.Spreadsheet,
                                                           c.GetAvailableUri(
                                                               @"../pivotTables/pivotTable{0}.xml"),
                                                           SpreadsheetConstants.PivotTableRelationship,
                                                           SpreadsheetConstants.PivotTableContentType))
                    {
                        c2.WriteElement(_definition);
                    c.SetString(OfficeConstants.RelationshipNamespace + "id", c2.Relationship.Id);
                    }

                }
                c.WriteOptionalElement(_pivotArea);
            }
        }
    }
}